package com.apes.scm.advanceOrder.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：预售计划
 *
 * @author HuJianLin
 * @create 2018-12-11 17:36
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = AdvanceSalePlan.CONST_TABLE_NAME)
public class AdvanceSalePlan extends Model {
    public static final String CONST_TABLE_NAME = "advance_sale_plan";

    @Id
    @Column(name = "id")
    @Comment("预售计划单号")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "预售计划单", prefix = "YS%(company)%(yy)%(month)", padding = 5, useDateRange=true)
    private String id;

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "LOCATION_ID", referencedColumnName = "id", nullable = false)
    private Location location;  //仓库库区

    @Temporal(TemporalType.DATE)
    @Column(name = "valid_from")
    @Comment("有效期起")
    private Date validFrom;

    @Temporal(TemporalType.DATE)
    @Column(name = "valid_period")
    @Comment("有效期止")
    private Date validPeriod;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

    @States(name = "订单状态", values = {
            @State(id = "sent", name = "申请"),
            @State(id = "approve", name = "已审核"),
            @State(id = "done", name = "已执行"),
            @State(id = "repeal", name = "已终止"),
            @State(id="executing",name="正在执行")})
    @Column(name = "state")
    @Comment("状态(create:创建,approve:审核,done:已执行,repeal:终止)")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole repealUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "repeal_date")
    @Comment("终止日期")
    private Date repealDate;

    @OneToMany(mappedBy = "advanceSalePlan", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<AdvanceSalePlanItem> advanceSalePlanItems = new HashSet(); //订单行

    public Set<AdvanceSalePlanItem> getAdvanceSalePlanItems() {
        for (AdvanceSalePlanItem item : advanceSalePlanItems) {
            item.setAdvanceSalePlan(this);
        }
        return advanceSalePlanItems;
    }


}
